<template>
	<view class="main uni-pa-4">
		<topic
		v-for="(item, index) in questionList"
		:key="item.id"
		:topic="item"
		:i="index"
		class="uni-mt-4"
		@setCollect="onSetCollect"
		/>
	</view>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue';
import topic from '../../components/topic.vue';
import request from '../../utils/request';
import type { QuestionItem } from '../../utils/type';
import { getInlineStrHtml } from '../../utils/utils';

interface Question {
	id: number;
	tenantId: number;
	categoryId: number;
	categoryName: string;
	questionsType: 1 | 2 | 3 | 4 | 5;
	sameFlg: string;
	subject: string;
	content: string;
	choiceContent: any;
	difficulty: number;
	video: string;
	score: string;
	createTime: string;
	createBy: number;
	updateTime: any;
	updateBy: number;
	imgs: Imgs[];
	answers: Answers[];
	videoQrCode: string;
	isDelete: number; 
}

interface Answers {
	id: number;
	questionsId: number;
	content: string;
	createTime: string; 
}

interface Imgs {
	id: number;
	questionsId: number;
	content: string;
	createTime: string;
	isDelete: number; 
}

const questionListData = ref<Question[]>([]);

const questionList = computed<QuestionItem[]>(() => questionListData.value.map(item => {
	return {
		...item,
		answers: (item.answers || []).map(item => getInlineStrHtml(item.content || '')),
		imgs: (item.imgs || []).map(item => item.content),
		choiceDtos: item.choiceContent ? item.choiceContent.split(",").map((str: string) => {
			const arr: string[] = str.split(":");
			return {
				item: arr[0] || '',
				itemContent: arr[1] || ''
			}
		}) : [],
		isCollect: 1,
		sort: 1
	}
}));

const onSetCollect = (isCollect: 1 | 0, questionItem: QuestionItem) => {
	if (isCollect == 0) {
		const index = questionListData.value.findIndex(item => item.id == questionItem.id);
		if (index > -1) questionListData.value.splice(index, 1);
	}
};

const getErrorList = async () => {
	try{
		questionListData.value = await request({
			method: 'GET',
			url: '/app/member-paper/questions/list',
			contentTypeIsForm: false,
		});
	} catch(e) {
		//TODO handle the exception
	}
};

// 点击删除
const view = (row: any) => {
	
};

onMounted(() => {
	getErrorList();
});
</script>
